package com.ijunfu.detail.thread._01_Thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2023/11/23 16:39
 * @Version: 1.0
 * @Motto  : 世界很大 世界很小
 *
 */
@Slf4j
public class _10Main {

    public static void main(String[] args) {

        Thread t1 = new Thread(()->{

            while(true) {

                try {
                    TimeUnit.SECONDS.sleep(1L);
                } catch (InterruptedException e) {          // InterruptedException 会清除中断标识
                    // 再次加上中断标识
                    Thread.currentThread().interrupt();
                }

                log.info("开始执行...");
                // 判断线程是否被打断
                boolean isInterrupted = Thread.currentThread().isInterrupted();     // true
                log.info("1. isInterrupted={}", isInterrupted);

                // 清除打断标记，让线程继续执行
                if(Thread.interrupted()) {
                    log.info("线程被中断，停止监控");
                    break;
                }

                log.info("监控中...");
            }


        });

        // 启动线程
        t1.start();

        // 模拟中断线程
        t1.interrupt();
    }
}
